from sqlalchemy import Column, String, Integer, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from init.mysql import engine

Base = declarative_base()


class StockStrong(Base):
    __tablename__ = 'stock_strong'
    id = Column(Integer, primary_key=True, autoincrement=True)
    qdate = Column(String(10))
    dm = Column(String(10), comment='代码')
    mc = Column(String(50), comment='名称')
    p = Column(String(20), comment='价格（元）')
    ztp = Column(String(20), comment='涨停价（元）')
    zf = Column(String(10), comment='涨幅（%）')
    cje = Column(String(20), comment='成交额（元）')
    lt = Column(String(20), comment='流通市值（元）')
    zsz = Column(String(20), comment='总市值（元）')
    zs = Column(String(10), comment='涨速（%）')
    nh = Column(Integer, comment='是否新高（0：否，1：是）')
    lb = Column(String(10), comment='量比')
    hs = Column(String(10), comment='换手率（%）')
    tj = Column(String(20), comment='涨停统计（x天/y板）')

    def __init__(self, qdate, dm, mc, p, ztp, zf, cje, lt, zsz, zs, nh, lb, hs, tj):
        self.qdate = qdate
        self.dm = dm
        self.mc = mc
        self.p = p
        self.ztp = ztp
        self.zf = zf
        self.cje = cje
        self.lt = lt
        self.zsz = zsz
        self.zs = zs
        self.nh = nh
        self.lb = lb
        self.hs = hs
        self.tj = tj

    @staticmethod
    def not_exist_insert(qdate, data_list):
        session = sessionmaker(bind=engine)()
        existing_stock_strong = session.query(StockStrong).filter_by(qdate=qdate).first()
        if existing_stock_strong:
            return
        for data in data_list:
            session.add(
                StockStrong(qdate=qdate, dm=data['dm'], mc=data['mc'], p=data['p'], ztp=data['ztp'], zf=data['zf'],
                            cje=data['cje'], lt=data['lt'], zsz=data['zsz'], zs=data['zs'], nh=data['nh'],
                            lb=data['lb'],
                            hs=data['hs'], tj=data['tj']))
        session.commit()
        session.close()


Base.metadata.create_all(engine)
